package cn.zmall.sso.service.impl;

import cn.zmall.common.jedis.JedisClient;
import cn.zmall.manager.dto.DtoUtil;
import cn.zmall.manager.dto.front.CartProduct;
import cn.zmall.manager.mapper.TbItemMapper;
import cn.zmall.manager.pojo.TbItem;
import cn.zmall.sso.service.CartService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author BinBini
 * @Date 2020/11/23 15:30
 * @Version 1.0
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private JedisClient jedisClient;
    @Value("Cart")
    private String CART_PRE;
    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public int addCart(long userId, long itemId, int num) {
        // key: 用户id field: 商品id value: 商品信息
        Boolean hexists = jedisClient.hexists(CART_PRE + ":" + userId, itemId + "");
        // 如果存在数量相加
        if(hexists) {
            String json = jedisClient.hget(CART_PRE + ":" + userId, itemId + "");
            if(json != null) {
                CartProduct cartProduct = new Gson().fromJson(json, CartProduct.class);
                cartProduct.setProductNum(cartProduct.getProductNum() + num);
                jedisClient.hset(CART_PRE + ":" + userId, itemId + "", new Gson().toJson(cartProduct));
            }else {
                return 0;
            }
        }
        // 如果不存在，根据商品id获取商品信息
        TbItem tbItem = tbItemMapper.selectByPrimaryKey(itemId);
        if(tbItem == null) {
            return 0;
        }
        CartProduct cartProduct = new DtoUtil().TbItemToCartProduct(tbItem);
        cartProduct.setProductNum((long) num);
        cartProduct.setChecked("1");
        jedisClient.hset(CART_PRE + ":" + userId, itemId + "", new Gson().toJson(cartProduct));
        return 1;
    }

    @Override
    public List<CartProduct> getCartList(long userId) {
        List<String> jsonList = jedisClient.hvals(CART_PRE + ":" + userId);
        List<CartProduct> cartProducts = new ArrayList<>();
        for (String json : jsonList) {
            CartProduct cartProduct = new Gson().fromJson(json, CartProduct.class);
            cartProducts.add(cartProduct);
        }
        return cartProducts;
    }

    @Override
    public int updateCartNum(long userId, long itemId, int num, String checked) {
        String json = jedisClient.hget(CART_PRE + ":" + userId, itemId + "");
        if(json == null) {
            return 0;
        }
        CartProduct cartProduct = new Gson().fromJson(json, CartProduct.class);
        cartProduct.setProductNum((long) num);
        cartProduct.setChecked(checked);
        jedisClient.hset(CART_PRE + ":" + userId, itemId + "", new Gson().toJson(cartProduct));
        return 1;
    }

    @Override
    public int checkAll(long userId, String checked) {
        List<String> jsonList = jedisClient.hvals(CART_PRE + ":" + userId);
        for(String json : jsonList) {
            CartProduct cartProduct = new Gson().fromJson(json, CartProduct.class);
            if("true".equals(checked)) {
                cartProduct.setChecked("1");
            }else if("false".equals(checked)) {
                cartProduct.setChecked("0");
            }else {
                return 0;
            }
            jedisClient.hset(CART_PRE + ":" + userId, cartProduct.getProductId() + "", new Gson().toJson(cartProduct));
        }
        return 1;
    }

    @Override
    public int delCartItem(long userId, long itemId) {
        jedisClient.hdel(CART_PRE + ":" + userId, itemId + "");
        return 1;
    }

    @Override
    public int delChecked(long userId) {
        List<String> jsonList = jedisClient.hvals(CART_PRE + ":" + userId);
        for(String json : jsonList) {
            CartProduct cartProduct = new Gson().fromJson(json, CartProduct.class);
            if("1".equals(cartProduct.getChecked())) {
                jedisClient.hdel(CART_PRE + ":" + userId, cartProduct.getProductId() + "");
            }
        }
        return 1;
    }
}
